package com.business.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.KeyGenerator;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.dao.MapDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.query.MapSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.utils.WebUtils;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.business.system.bean.ApplyBean;
import com.business.system.bean.BackFeeLogBean;
import com.business.system.bean.WxUserBean;
import com.business.system.util.DBTools;
import com.business.system.util.PageUtil;

/**
 * Created by jiangwenfeng on 2018/4/14.
 */
@RestController
@RequestMapping("apply")
public class ApplyAction extends BaseAction{

    @RequestMapping("getAngelApplyList.do")
    public PageBean getAngelApplyList(Service service, HttpServletRequest request) {
        PageBean page = PageUtil.page(request);
        StringBuilder sql = new StringBuilder();
        sql.append("select t1.UPDATE_TIME as applyTime,t1.REMARK as remark,t1.ID as applyId,t1.*,t2.*,t3.USER_NAME as formName from apply t1 left join wx_user t2 on t1.USER_ID = t2.ID")
                .append(" left join wx_user t3 on t2.FROM_ID = t3.ID")
                .append(" where 1 = 1");
        Map<String, Object> params = WebUtils.getRequestData(request);
        List<Object> condition = new ArrayList<>();
        if(params.get("applyRes")!=null&&!"".equals(params.get("applyRes"))){
            sql.append(" and t1.APPLY_STATUS =?");
            condition.add(params.get("applyRes").toString());
        }
        if(params.get("payRes")!=null&&!"".equals(params.get("payRes"))){
            sql.append(" and t1.PAY_STATUS =?");
            condition.add(params.get("payRes").toString());
        }
        if(params.get("type")!=null&&!"".equals(params.get("type"))){
            sql.append(" and t1.TYPE =?");
            condition.add(Integer.parseInt(params.get("type").toString()));
        }

        if (params.containsKey("content")) {
            sql.append(" and (t2.USER_NAME like '%" + params.get("content") + "%' or t2.MOBILE like '%"+params.get("content")+"%')");
        }
        sql.append(" order by t1.UPDATE_TIME desc");

        MapDAO dao = new MapDAO(service);
        MapSQL query = dao.getQuerySQL();
        query.setSQL(sql.toString());
        query.setParameters(condition);
        query.setPage(page);
        List<Map<String, Object>> dataList = dao.list();
        if(dataList.size()>0) {
            for (Map<String, Object> data : dataList) {
                String brands = "";
                String applyId = data.get("applyId").toString();
                String sql1 = "select t2.* from user_brand t1 left join brand t2 on t1.brand_id = t2.ID where 1 =1 and t1.APPLY_ID=?";
                query.setSQL(sql1);
                query.addParameter(applyId);
                List<Map<String, Object>> list = dao.list();
                if(list.size()>0) {
                    for (int i = 0; i < list.size(); i++) {
                        if (i != list.size() - 1) {
                            brands += list.get(i).get("BRAND_NAME").toString() + ",";
                        } else {
                            brands += list.get(i).get("BRAND_NAME").toString();
                        }
                    }
                }
                data.put("brand", brands);
            }
        }
        return page;
    }

    /**
     * 根据userid和area获得对应的品牌
     * jiangwenfeng
     * 2018-04-16 22:18
     */
    @RequestMapping("getBrandListByUser.do")
    public List<Map<String, Object>> getBrandListByUser(Service service, HttpServletRequest request) {
        StringBuilder sql = new StringBuilder();
        sql.append("select t2.* from user_brand t1 left join brand t2 on t1.brand_id = t2.ID where 1 =1");
        Map<String, Object> params = WebUtils.getRequestData(request);
        List<Object> condition = new ArrayList<>();
        if(params.get("userId")!=null&&!"".equals(params.get("userId"))){
            sql.append(" and t1.USER_ID =?");
            condition.add(params.get("userId").toString());
        }
        if(params.get("code")!=null&&!"".equals(params.get("code"))){
            sql.append(" and t1.area_code =?");
            condition.add(params.get("code").toString());
        }

        return DBTools.getDataList(service, sql.toString(),condition);

    }

    /**
     * 根据userId获得用户信息
     * jiangwenfeng
     * 2018-04-16 22:18
     */
    @RequestMapping("getUserInfo.do")
    public Map<String, Object> getUserInfo(Service service, HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getRequestData(request);
        MapDAO dao = new MapDAO(service);
        MapSQL query = dao.getQuerySQL();
        String sql="select t1.* from WX_USER t1 where ID =?";
        query.setSQL(sql);
        query.addParameter(params.get("userId").toString());
        Map<String, Object> data = dao.get();
        return data;
    }

    /**
     * @Description:保存审核状态
     */
    @RequestMapping("saveApplyState.do")
    public int saveApplyState(Service service, HttpServletRequest request,String applyId,String type) {
        BeanDAO dao = new BeanDAO(service);
        BeanSQL query = dao.getQuerySQL();
        String sql = "update APPLY set APPLY_STATUS =? where ID=?";
        query.setSQL(sql);
        query.addParameter(Integer.parseInt(type));
        query.addParameter(applyId);
        return dao.update();
    }

    /**
     * @Description:保存付款状态
     * @Author: jiangwenfeng
     * @Date: 13:10 2018/4/16
     */
    @RequestMapping("savePayState.do")
    public int savePayState(Service service, HttpServletRequest request,String applyId,String type,String fee) throws Exception {
        String sql = "select * from apply where ID=?";
        ApplyBean bean = DBTools.getBean(service,ApplyBean.class,sql,applyId);
        bean.setPayStatus(Integer.parseInt(type));
        if(fee!=null&&!"".equals(fee)) {
            bean.setRealFee(Double.parseDouble(fee));
        }
        int i = DBUtils.update(service,bean);
        int count=0;
        if(i>0){
            if(Integer.parseInt(type)==3) {
                WxUserBean wxUserBean = DBTools.getBean(service, WxUserBean.class, "select t1.* from WX_USER t1 LEFT JOIN wx_user t2 ON t1.ID=t2.FROM_ID WHERE t2.ID=?", bean.getUserId());
                if (wxUserBean != null) {
//                    BackFeeLogBean backFeeLogBean = new BackFeeLogBean();
//                    backFeeLogBean.setId(KeyGenerator.getSystemUUID());
//                    backFeeLogBean.setUserId(wxUserBean.getId());
//                    backFeeLogBean.setFromUserId(bean.getUserId());
//                    backFeeLogBean.setResourceId(applyId);
//                    backFeeLogBean.setType(2);
//                    backFeeLogBean.setState(1);
//                    backFeeLogBean.setBackFee(bean.getRealFee());
//                    count = DBTools.insertBean(service, backFeeLogBean);
//                    if(count>0){
//                        i = 1;
//                    }else{
//                        i = 0;
//                    }
                   boolean f = backFeeLogService.insertApplyFeeLog(service,bean,wxUserBean);
                    if(f==true){
                        i = 1;
                    }else{
                        i = 0;
                    }
                }
            }
        }
        return i;
    }

    /**
     * @Description:根据applyId查询凭证图片信息
     * @Author: jiangwenfeng
     * @Date: 13:10 2018/4/18
     */
    @RequestMapping("getApplyFile.do")
    public Map<String, Object> getApplyFile(Service service, HttpServletRequest request) {
        Map<String, Object> params = WebUtils.getRequestData(request);
        MapDAO dao = new MapDAO(service);
        MapSQL query = dao.getQuerySQL();
        String sql="select t1.* from apply_file t1 where apply_id =? order by t1.UPDATE_TIME desc limit 1";
        query.setSQL(sql);
        query.addParameter(params.get("applyId").toString());
        Map<String, Object> data = dao.get();
        return data;
    }
}
